namespace Plugins.Engine
{
    using System;
    using Plugins.Interfaces;
    using Plugins.Serializer;

    [Serializable]
    public class PluginConfigurationData : IPluginAssemblyConfiguration, IEquatable<IPluginAssemblyConfiguration>, IPluginSerializable
    {
        private string _name;
        private string _publicKeyToken;
        internal static readonly IPluginRuntimeFormatter Formatter = new XmlFormatter();
        private static readonly IPluginStringSerializer<IPluginAssemblyConfiguration> XmlStringSerializer = new PluginUnicodeStringSerializer<IPluginAssemblyConfiguration>();

        private PluginConfigurationData() { }

        internal PluginConfigurationData(IPluginAssemblyConfiguration plugin)
        {
            if (plugin != null)
            {
                this._name = plugin.Name;
                this._publicKeyToken = plugin.PublicKeyToken;
            }
        }

        bool IEquatable<IPluginAssemblyConfiguration>.Equals(IPluginAssemblyConfiguration plugin)
        {
            return object.ReferenceEquals(this, plugin);
        }

        public string Serialize()
        {
            return XmlStringSerializer.Serialize(this, Formatter);
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }

        public string PublicKeyToken 
        {
            get 
            {
                return this._publicKeyToken;
            }
            set
            {
                this._publicKeyToken = value;
            }
        }

        private class XmlFormatter : PluginTypeSerializer<PluginConfigurationData>
        {
            public override void AddOverrides(IPluginTypeFormatter formatter)
            {
                AddType(formatter, "plugin");
                AddAttribute(formatter, "Name", "assemblyName");
                AddAttribute(formatter, "PublicKeyToken", "publicKeyToken");
            }
        }
    }
}

